package com.mall.product.server.service.impl;


import com.mall.common.core.domain.Result;
import com.mall.product.common.domain.PmsSkuImages;
import com.mall.product.common.domain.PmsSkuInfo;
import com.mall.product.common.domain.PmsSpuInfo;
import com.mall.product.common.domain.PmsSpuInfoDesc;
import com.mall.product.server.config.ProductThreadPoolConfig;
import com.mall.product.server.mapper.DetailsMapper;
import com.mall.product.server.service.*;
import com.mall.product.server.vo.AttrGroupVo;
import com.mall.product.server.vo.ItemInfoVo;
import com.mall.product.server.vo.SaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class DetailsServiceImpl implements DetailsService {

    @Autowired
    private IPmsSkuInfoService SkuInfoService;
    @Autowired
    private IPmsSpuInfoService spuInfoService;
    @Autowired
    private IPmsSpuInfoDescService spuInfoDescService;
    @Autowired
    private IPmsSpuInfoService SpuInfoService;
    @Autowired
    private IPmsSkuImagesService SkuImagesService;
    @Autowired
    private IPmsSkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private IPmsCategoryService categoryService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private DetailsMapper detailsMapper;

    /**
     * 查询商品详情页
     *
     * @param skuId
     * @return
     */
    @Override
    public Result<ItemInfoVo> getDetailsBySkuId(Long skuId) throws InterruptedException, ExecutionException {
        //1.创建返回对象
        ItemInfoVo itemInfoVo = new ItemInfoVo();

        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //2.根据skuId查询skuInfo
            PmsSkuInfo pmsSkuInfo = SkuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, threadPoolExecutor);


        //2.1查询spuInfo
        CompletableFuture<Void> spuInfoCompletableFuture = pmsSkuInfoCompletableFuture.thenAccept(pmsSkuInfo -> {
            PmsSpuInfo pmsSpuInfo = spuInfoService.selectPmsSpuInfoById(pmsSkuInfo.getSpuId());
        });


        //2.根据skuId查询spu下的所有sku销售属性
        CompletableFuture<Void> skuSaleAttrValueCompletableFuture = CompletableFuture.runAsync(() -> {
            List<SaleAttrVo> saleAttrVos = skuSaleAttrValueService.selectSkuSaleAttrsBySkuId(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);
        }, threadPoolExecutor);


        //3.根据catelogId查询spu商品属性分组对象集合
        CompletableFuture<Void>AttrGroupVosCompletableFuture = pmsSkuInfoCompletableFuture.thenAccept(pmsSkuInfo -> {
            List<AttrGroupVo> attrGroupVos = skuSaleAttrValueService.selectPmsSpuAttrGroupVoByCatelogId(pmsSkuInfo.getCatalogId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        });

        //4.根据skuId查询skuImageList
        CompletableFuture<Void> SkuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            List<PmsSkuImages> pmsSkuImages = SkuImagesService.selectPmsSkuImagesBySkuId(skuId);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        }, threadPoolExecutor);


        //5.根据spuId查询desc对象
        CompletableFuture<Void> spuInfoDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAccept(pmsSkuInfo -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = spuInfoDescService.selectPmsSpuInfoDescBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        });
        //返回结果集
        CompletableFuture.allOf(
                pmsSkuInfoCompletableFuture,
                spuInfoCompletableFuture,
                skuSaleAttrValueCompletableFuture,
                AttrGroupVosCompletableFuture,
                SkuImagesCompletableFuture,
                spuInfoDescCompletableFuture
                ).get();
        return Result.success(itemInfoVo);
    }


    /**
     * SELECT
     *
     * 	sale_attr.sku_id,
     * 	sale_attr.attr_id,
     * 	sale_attr.attr_name,
     * 	sale_attr.attr_value
     * FROM
     * 	pms_sku_sale_attr_value AS sale_attr
     * 	JOIN pms_sku_info sku ON sale_attr.sku_id = sku.sku_id
     * 	JOIN pms_spu_info spu_info ON sku.spu_id = spu_info.id
     * 	JOIN pms_attr attr ON sale_attr.attr_id = attr.attr_id
     * WHERE
     * 	sku.spu_id = (
     * 	SELECT
     * 		id AS spu_id
     * 	FROM
     * 		pms_spu_info
     * 	WHERE
     * 	id = 3
     * 	)
     */
}
